package com.ruoyi.web.controller.inspection;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.InspectionPlanTaskDetailsVO;
import com.ruoyi.system.domain.vo.PreventionSecurityRiskVO;
import com.ruoyi.system.entity.*;
import com.ruoyi.system.mapper.InspectionPointMapper;
import com.ruoyi.system.mapper.PreventionHiddenInfoRecordMapper;
import com.ruoyi.system.service.InspectionPlanTaskDetailsFileService;
import com.ruoyi.system.service.InspectionPlanTaskDetailsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 计划任务详情表(InspectionPlanTaskDetails)表控制层
 * @since 2022-11-28 17:55:43
 */
@RestController
@RequestMapping("inspectionPlanTaskDetails")
public class InspectionPlanTaskDetailsController extends BaseController {

    @Autowired
    private InspectionPlanTaskDetailsService inspectionPlanTaskDetailsService;
    @Autowired
    private PreventionHiddenInfoRecordMapper preventionHiddenInfoRecordMapper;
    @Autowired
    private InspectionPointMapper inspectionPointMapper;
    @Autowired
    private InspectionPlanTaskDetailsFileService inspectionPlanTaskDetailsFileService;

    /**
     * 分页查询
     * @param inspectionPlanTaskDetails 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(InspectionPlanTaskDetails inspectionPlanTaskDetails) {
        startPage();
        List<InspectionPlanTaskDetails> inspectionPlanTaskDetailsList = this.inspectionPlanTaskDetailsService.queryByPage(inspectionPlanTaskDetails);
        if (!CollectionUtils.isEmpty(inspectionPlanTaskDetailsList)){
            for (InspectionPlanTaskDetails taskDetails : inspectionPlanTaskDetailsList) {
                List<PreventionHiddenInfoRecord> recordList = this.preventionHiddenInfoRecordMapper.getPlanTaskDetails(taskDetails.getPlanTaskId(),taskDetails.getId());
                if (!CollectionUtils.isEmpty(recordList)){
                    for (PreventionHiddenInfoRecord infoRecord : recordList) {
                        if (!ObjectUtils.isEmpty(infoRecord)){
                            taskDetails.setCheckState("有隐患");
                            this.inspectionPlanTaskDetailsService.update(taskDetails);
                        }
                    }
                }
                InspectionPoint pointEntity = inspectionPointMapper.queryById(taskDetails.getPointId());
                if (ObjectUtils.isNotEmpty(pointEntity)){
                    taskDetails.setPointCode(pointEntity.getPointCode());
                }

                // 处理附件表
                List<InspectionPlanTaskDetailsFile> detailsFileList = new ArrayList<>();
                String fileId = taskDetails.getHiddenFileId();
                if (StringUtils.isNotEmpty(fileId)) {
                    fileId = fileId.substring(0, fileId.length() - 1);
                    if (fileId.contains(",")) {
                        String[] split = fileId.split(",");
                        List<String> id = Arrays.asList(split);
                        if (StringUtils.isNotEmpty(id)){
                            for (String s : id) {
                                InspectionPlanTaskDetailsFile detailsFile = inspectionPlanTaskDetailsFileService.queryById(Integer.valueOf(s));
                                if (!org.springframework.util.ObjectUtils.isEmpty(detailsFile)){
                                    detailsFileList.add(detailsFile);
                                }
                            }
                        }
                    } else {
                        InspectionPlanTaskDetailsFile detailsFile = inspectionPlanTaskDetailsFileService.queryById(Integer.valueOf(fileId));
                        if (!org.springframework.util.ObjectUtils.isEmpty(detailsFile)){
                            detailsFileList.add(detailsFile);
                        }
                    }
                }
                taskDetails.setPlanTaskDetailsFileList(detailsFileList);

            }
        }
        return getDataTable(inspectionPlanTaskDetailsList);
    }


    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.inspectionPlanTaskDetailsService.queryById(id));
    }

    /**
     * 新增数据
     * @param inspectionPlanTaskDetails 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    public AjaxResult add(@RequestBody InspectionPlanTaskDetails inspectionPlanTaskDetails) {
        return AjaxResult.success(this.inspectionPlanTaskDetailsService.insert(inspectionPlanTaskDetails));
    }

    /**
     * 编辑数据
     * @param inspectionPlanTaskDetails 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody InspectionPlanTaskDetails inspectionPlanTaskDetails) {
        return AjaxResult.success(this.inspectionPlanTaskDetailsService.update(inspectionPlanTaskDetails));
    }

    /**
     * 删除数据
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.inspectionPlanTaskDetailsService.deleteById(id));
    }


    /**
     * 完成单个点位任务——任务完成
     */
    @PostMapping("/completeTask")
    public AjaxResult completeTask(@RequestBody InspectionPlanTaskDetails inspectionPlanTaskDetails) {
        inspectionPlanTaskDetailsService.completeTask(inspectionPlanTaskDetails);
        return AjaxResult.success();
    }

    @Log(title = "巡检任务详情(", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionPlanTaskDetailsVO inspectionPlanTaskDetailsVO)
    {
        List<InspectionPlanTaskDetailsVO> list = inspectionPlanTaskDetailsService.queryAllVO(inspectionPlanTaskDetailsVO);
        ExcelUtil<InspectionPlanTaskDetailsVO> util = new ExcelUtil<>(InspectionPlanTaskDetailsVO.class);
        util.exportExcel(response, list, "巡检任务详情(");
    }
}

